import {
    request
} from "../../request/index.js";
var redis = require('../../utils/redis').default;
const login_time = 7 * 24 * 60 * 60;
Page({
    data: {},
    onLoad() {},
    login() {
        wx.getUserProfile({
            desc: '用于用户资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (e) => {
                // 获取用户信息
                const {
                    encryptedData,
                    rawData,
                    iv,
                    signature,
                    userInfo
                } = e;
                wx.login({
                    success: async (res) => {
                        if (res.code) {
                            // 获取小程序登录成功后的code
                            const { code } = res;
                            const loginParams = {
                                encryptedData,
                                rawData,
                                iv,
                                signature,
                                userInfo,
                                code
                            };
                            const returnResult = await request({
                                url: "/users/wxLogin",
                                data: loginParams,
                                method: "post"
                            })
                            redis.put('token', returnResult.data.data.token, login_time);
                            redis.put('userId', returnResult.data.data.userId, login_time);
                            redis.put('userInfo', returnResult.data.data.userInfo, login_time);
                            wx.navigateBack({
                                delta: 1,
                            })
                        } else {
                            console.log("登录失败！");
                        }
                    }
                })
            }
        })
    }
})